<template>
	<view class="wrap">
		<u-toast ref="uToast" />
		<view class="top">
			<view class="item">
				<view class="left">收货人</view>
				<input type="text" v-model="addrData.consignee" placeholder-class="line" placeholder="请填写收货人姓名" />
			</view>
			<view class="item">
				<view class="left">手机号码</view>
				<input type="text" v-model="addrData.phone" placeholder-class="line" placeholder="请填写收货人手机号" />
			</view>
			<view class="item" @tap="showRegionPicker">
				<view class="left">所在地区</view>
				<input disabled type="text" v-model="addrData.province+' '+addrData.city+' '+addrData.district" placeholder-class="line" placeholder="省市区县、乡镇等" />
			</view>
			<view class="item address">
				<view class="left">详细地址</view>
				<textarea type="text" v-model="addrData.address" placeholder-class="line" placeholder="街道、楼牌等" />
			</view>
			<!-- <view class="site-clipboard">
				<textarea placeholder-class="line" value="" placeholder="粘贴文本,可自动识别姓名和地址等" />
				<view class="clipboard">
					地址粘贴板
					<u-icon name="arrow-down" class="icon" :size="20"></u-icon>
				</view>
			</view> -->
		</view>
		<view class="bottom">
			<view class="item">
				<view class="left">标签</view>
				<input type="text" v-model="addrData.label" placeholder-class="line" placeholder="家,公司,或者您自定义" />
			</view>
			<view class="default">
				<view class="left">
					<view class="set">设置默认地址</view>
					<view class="tips">提醒：每次下单会默认推荐该地址</view>
				</view>
				<view class="right"><u-switch v-model="swit" @change="change" ></u-switch></view>
			</view>
		</view>
		<view style="margin-top:20rpx;padding-top:20rpx;height:120rpx;width: 100%;background-color: #FFFFFF;">
			<view style="width: 50%;margin-left: 25%;">
				<u-button type="primary" @click="submit">确定</u-button>
			</view>
		</view>
		<u-picker mode="region" ref="uPicker" v-model="show"  @confirm="handlePicker"/>
	</view>
</template>

<script>
export default {
	data() {
		return {
			addrData:{
				province:'',
				city:'',
				district:''
			},
			show: false,
			swit:false,
			status:1,
		};
	},
	onLoad(e) {
		if(e.id!==undefined){
			this.addrData=e
			this.swit=(e.status==0)
			this.status=e.status
		}
		
	},
	methods: {
		submit(){
			let mainId=false
			//如果另外选中其他默认地址
			if(this.status==1&&this.addrData.status==0){
				let pages = getCurrentPages();
				let prePage = pages[pages.length - 2];
				let addrData=prePage.data.siteList.filter(t=>t.status==0)
				
				if(addrData.length>0&&addrData[0].id!==this.addrData.id){
					mainId=addrData[0].id
				}
			}
			this.$u.api.submitAddress(mainId?Object.assign(this.addrData,{oldMainId:mainId}):this.addrData).then(res=>{
				this.$refs.uToast.show({
					title: '提交成功',
						// 如果不传此type参数，默认为default，也可以手动写上 type: 'default'
					type: 'success', 
						// 如果不需要图标，请设置为false
					icon: false,
					duration:500,
					back:true,
				});
			})
		},
		handlePicker(e){
			this.addrData.province=e.province.label
			this.addrData.city=e.city.label
			this.addrData.district=e.area.label
		},
		
		change(e){
			if(e)
				this.addrData.status=0
			else
				this.addrData.status=1
		},
		setDefault() {},
		showRegionPicker() {
			this.show = true;
		}
	}
};
</script>

<style lang="scss" scoped>
/deep/ .line {
	color: $u-light-color;
	font-size: 28rpx;
}

.wrap {
	background-color: #f2f2f2;
	.top {
		background-color: #ffffff;
		border-top: solid 2rpx $u-border-color;
		padding: 22rpx;
		.item {
			display: flex;
			font-size: 32rpx;
			line-height: 100rpx;
			align-items: center;
			border-bottom: solid 2rpx $u-border-color;
			.left {
				width: 180rpx;
			}
			input {
				text-align: left;
			}
		}
		
		.address {
			padding: 20rpx 0;
			textarea {
				// width: 100%;
				height: 150rpx;
				background-color: #f7f7f7;
				line-height: 60rpx;
				margin: 40rpx auto;
				padding: 20rpx;
			}
		}
		.site-clipboard {
			padding-right: 40rpx;
			textarea {
				// width: 100%;
				height: 150rpx;
				background-color: #f7f7f7;
				line-height: 60rpx;
				margin: 40rpx auto;
				padding: 20rpx;
			}
			.clipboard {
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 26rpx;
				color: $u-tips-color;
				height: 80rpx;
				.icon {
					margin-top: 6rpx;
					margin-left: 10rpx;
				}
			}
		}
	}
	.bottom {
		margin-top: 20rpx;
		padding: 40rpx;
		padding-right: 0;
		background-color: #ffffff;
		font-size: 28rpx;
		.tag {
			display: flex;
			.left {
				width: 160rpx;
			}
			.right {
				display: flex;
				flex-wrap: wrap;
				.tags {
					width: 140rpx;
					padding: 16rpx 8rpx;
					border: solid 2rpx $u-border-color;
					text-align: center;
					border-radius: 50rpx;
					margin: 0 10rpx 20rpx;
					display: flex;
					font-size: 28rpx;
					align-items: center;
					justify-content: center;
					color: $u-content-color;
					line-height: 1;
				}
				.plus {
					//padding: 10rpx 0;
				}
			}
		}
		.default {
			margin-top: 50rpx;
			display: flex;
			justify-content: space-between;
			border-bottom: solid 2rpx $u-border-color;
			line-height: 64rpx;
			.tips {
				font-size: 24rpx;
			}
			.right {
			}
		}
	}
}
</style>
